﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "RadioButtonGroupDefinition.h"
#include "ControlDefinition.h"
#include "GbmpNew.h"
#include "RadioButtonGroupEvent.h"
#include "FindByIdPredicate.h"
#include "ControlDefinitionChangeEvent.h"
#include "IRadioButtonGroupHandler.h"
#include <algorithm>
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

namespace gcmp
{
RadioButtonDefinition::RadioButtonDefinition(const std::wstring& id, const std::wstring& caption, const std::wstring& tooltip)
    :m_pBasicControlDefinition(NEW_AS_OWNER_PTR(ControlDefinition, id, caption, tooltip))
    , m_pControlHandler(nullptr)
{
}

std::wstring RadioButtonDefinition::GetId() const
{
    return m_pBasicControlDefinition->GetId();
}

std::wstring RadioButtonDefinition::GetCaption() const
{
    return m_pBasicControlDefinition->GetCaption();
}

std::wstring RadioButtonDefinition::GetTooltip() const
{
    return m_pBasicControlDefinition->GetTooltip();
}

bool RadioButtonDefinition::IsVisible() const
{
    if (m_pControlHandler)
    {
        return m_pControlHandler->IsVisible(GetId());
    }
    return m_pBasicControlDefinition->IsVisible();
}

bool RadioButtonDefinition::IsEnabled() const
{
    if (m_pControlHandler)
    {
        return m_pControlHandler->IsEnabled(GetId());
    }
    return m_pBasicControlDefinition->IsEnabled();
}

bool RadioButtonDefinition::SetEnabled(bool enabled)
{
    return m_pBasicControlDefinition->SetEnabled(enabled);
}

bool RadioButtonDefinition::SetVisible(bool visible)
{
    return m_pBasicControlDefinition->SetVisible(visible);
}

bool RadioButtonDefinition::SetControlHandler(const IRadioButtonGroupHandler* pControlHandler)
{
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pControlHandler, L"pControlHandler为空",L"GDMPLab",L"2024-03-30");
    m_pControlHandler = const_cast<IRadioButtonGroupHandler*>(pControlHandler);
    return true;
}

bool RadioButtonDefinition::BlockControlHandler(bool needBlock)
{
    return true;
}

RadioButtonGroupDefinition::RadioButtonGroupDefinition(const std::wstring& id, const std::wstring& caption)
    : m_pBasicControlDefinition(NEW_AS_OWNER_PTR(ControlDefinition, id, caption, caption))
{
    m_pDefinitionChangeEvent = NEW_AS_OWNER_PTR(ControlDefinitionChangeEvent, L"ControlDefinitionChangeEvent", this);
    m_pRadioButtonGroupEvent = NEW_AS_OWNER_PTR(RadioButtonGroupEvent, L"RadioButtonGroupEvent", this);
}

RadioButtonGroupDefinition::~RadioButtonGroupDefinition()
{
}

std::wstring RadioButtonGroupDefinition::GetId() const
{
    return m_pBasicControlDefinition->GetId();
}

std::wstring RadioButtonGroupDefinition::GetCaption() const
{
    return m_pBasicControlDefinition->GetCaption();
}

std::wstring RadioButtonGroupDefinition::GetTooltip() const
{
    return m_pBasicControlDefinition->GetTooltip();
}

bool RadioButtonGroupDefinition::AddRadioButton(const std::wstring& id, const std::wstring& caption, const std::wstring& tooltip)
{
    auto search = std::find_if(m_radioButtonDefinitions.begin(), m_radioButtonDefinitions.end(), FindPtrBIdPredicate<OwnerPtr<RadioButtonDefinition>>(id));
    DBG_WARN_AND_RETURN_FALSE_UNLESS(search == m_radioButtonDefinitions.end(), L"RadioButtonGroup内RadioButton名字必须唯一",L"GDMPLab",L"2024-03-30");

    OwnerPtr<RadioButtonDefinition> pRadioButtonDefine = NEW_AS_OWNER_PTR(RadioButtonDefinition, id, caption, tooltip);
    RadioButtonDefinition* define = pRadioButtonDefine.get();
    m_radioButtonDefinitions.emplace_back(TransferOwnership(pRadioButtonDefine));

    ControlDefinitionChangeEventArgs arguments(ControlDefinitionChangeEventType::Add);
    arguments.SetControlDefinition(define);
    m_pDefinitionChangeEvent->Emit(&arguments);

    
    if (m_currentRadioButtonId.empty())
    {
        m_currentRadioButtonId = id;
    }

    return true;
}

bool RadioButtonGroupDefinition::InsertRadioButton(const std::wstring& refName, const std::wstring& id, const std::wstring& caption, const std::wstring& tooltip)
{
    auto search = std::find_if(m_radioButtonDefinitions.begin(), m_radioButtonDefinitions.end(), FindPtrBIdPredicate<OwnerPtr<RadioButtonDefinition>>(id));
    DBG_WARN_AND_RETURN_FALSE_UNLESS(search == m_radioButtonDefinitions.end(), L"RadioButtonGroup内RadioButton名字必须唯一",L"GDMPLab",L"2024-03-30");

    search = std::find_if(m_radioButtonDefinitions.begin(), m_radioButtonDefinitions.end(), FindPtrBIdPredicate<OwnerPtr<RadioButtonDefinition>>(refName));
    DBG_WARN_AND_RETURN_FALSE_UNLESS(search != m_radioButtonDefinitions.end(), L"RadioButtonGroup内没有找到插入参考的名字",L"GDMPLab",L"2024-03-30");
    int index = static_cast<int>(std::distance(m_radioButtonDefinitions.begin(), search));

    OwnerPtr<RadioButtonDefinition> pRadioButtonDefine = NEW_AS_OWNER_PTR(RadioButtonDefinition, id, caption, tooltip);
    RadioButtonDefinition* define = pRadioButtonDefine.get();
    m_radioButtonDefinitions.insert(search, TransferOwnership(pRadioButtonDefine));

    ControlDefinitionChangeEventArgs arguments(ControlDefinitionChangeEventType::Insert);
    arguments.SetControlDefinition(define);
    arguments.SetDeleteOrInsertControlDefinitionIndex(index);
    m_pDefinitionChangeEvent->Emit(&arguments);

    return true;
}

bool RadioButtonGroupDefinition::DeleteRadioButton(const std::wstring& id)
{
    auto search = std::find_if(m_radioButtonDefinitions.begin(), m_radioButtonDefinitions.end(), FindPtrBIdPredicate<OwnerPtr<RadioButtonDefinition>>(id));
    DBG_WARN_AND_RETURN_FALSE_UNLESS(search != m_radioButtonDefinitions.end(), L"RadioButtonGroup内没找到要删除的名字",L"GDMPLab",L"2024-03-30");
    
    int index = static_cast<int>(std::distance(m_radioButtonDefinitions.begin(), search));

    m_radioButtonDefinitions.erase(search);
    if (id == m_currentRadioButtonId)
    {
        if (m_radioButtonDefinitions.empty())
        {
            m_currentRadioButtonId.clear();
        }
        else
        {
            m_currentRadioButtonId = m_radioButtonDefinitions.front()->GetId();
        }
    }

    ControlDefinitionChangeEventArgs arguments(ControlDefinitionChangeEventType::Delete);
    arguments.SetDeleteOrInsertControlDefinitionIndex(index);
    m_pDefinitionChangeEvent->Emit(&arguments);
    return true;
}

void RadioButtonGroupDefinition::DeleteAllRadioButtons()
{
    m_radioButtonDefinitions.clear();
    m_currentRadioButtonId.clear();
    ControlDefinitionChangeEventArgs arguments(ControlDefinitionChangeEventType::DeleteAll);
    m_pDefinitionChangeEvent->Emit(&arguments);
}

std::vector<const IRadioButtonDefinition*> RadioButtonGroupDefinition::GetAllRadioButtons() const
{
    std::vector<const IRadioButtonDefinition*> result;
    for (auto& define : m_radioButtonDefinitions)
        result.push_back(define.get());
    return result;
}

std::wstring RadioButtonGroupDefinition::GetCurrentRadioButton() const
{
    auto search = std::find_if(m_radioButtonDefinitions.begin(), m_radioButtonDefinitions.end(), FindPtrBIdPredicate<OwnerPtr<RadioButtonDefinition>>(m_currentRadioButtonId));
    DBG_WARN_AND_RETURN_FALSE_UNLESS(search != m_radioButtonDefinitions.end(), L"RadioButtonGroup内没有要找的名字",L"GDMPLab",L"2024-03-30");
    return (*search)->GetId();
}

bool RadioButtonGroupDefinition::SetCurrentRadioButton(const std::wstring& id)
{
    auto search = std::find_if(m_radioButtonDefinitions.begin(), m_radioButtonDefinitions.end(), FindPtrBIdPredicate<OwnerPtr<RadioButtonDefinition>>(id));
    DBG_WARN_AND_RETURN_FALSE_UNLESS(search != m_radioButtonDefinitions.end(), L"RadioButtonGroup内没有要找的名字",L"GDMPLab",L"2024-03-30");
    if (m_currentRadioButtonId != id)
    {
        m_currentRadioButtonId = id;
        EmitDefineChangeEvent();
        RadioButtonGroupEventArgs args(RadioButtonGroupEventType::RadioButtonGroupStateChanged, id);
        args.SetStateChangeByUserFlag(RadioButtonGroupStateChangeFlag::IsChangeByUser());
        RadioButtonGroupEvent* pEvent = dynamic_cast<RadioButtonGroupEvent*>(GetEvent());
        if (pEvent)
        {           
            pEvent->Emit(&args);
        }
        //DBG_WARN_AND_RETURN_FALSE_UNLESS(m_pHandler, L"RadioButtonGroup控件处理者未设置", L"GDMPLab", L"2024-03-30");
        if (m_handler)
        {
            m_handler.Process(&args);
        }
    }
    
    return true;
}

IRadioButtonGroupEvent* RadioButtonGroupDefinition::GetEvent() const
{
    return const_cast<IRadioButtonGroupEvent*>(m_pRadioButtonGroupEvent.get());
}

bool RadioButtonGroupDefinition::IsVisible() const
{
    std::vector<const IRadioButtonDefinition*> allRadioButtons = GetAllRadioButtons();
    for (auto iter : allRadioButtons)
    {
        if (iter && iter->IsVisible())
        {
            return true;
        }
    }
    return false;
}

bool RadioButtonGroupDefinition::SetEnabled(const std::wstring& id, bool enabled)
{
    auto search = std::find_if(m_radioButtonDefinitions.begin(), m_radioButtonDefinitions.end(), FindPtrBIdPredicate<OwnerPtr<RadioButtonDefinition>>(id));
    DBG_WARN_AND_RETURN_FALSE_UNLESS(search != m_radioButtonDefinitions.end(), L"m_radioButtonDefinitions内没有要找的名字",L"GDMPLab",L"2024-03-30");
    DBG_WARN_AND_RETURN_FALSE_UNLESS((*search), L"(*search)为空",L"GDMPLab",L"2024-03-30");
    if ((*search)->IsEnabled() != enabled)
    {
        bool res = (*search)->SetEnabled(enabled);
        if (res)
        {
            EmitDefineChangeEvent();
            return true;
        }
    }
    return false;
}

gcmp::IControlDefinitionChangeEvent* RadioButtonGroupDefinition::GetControlDefinitionChangeEvent() const
{
    return const_cast<ControlDefinitionChangeEvent*>(m_pDefinitionChangeEvent.get());
}

void RadioButtonGroupDefinition::EmitDefineChangeEvent()
{
    ControlDefinitionChangeEventArgs arguments(ControlDefinitionChangeEventType::Other);
    arguments.SetModifyControlDefinitionId(m_currentRadioButtonId);
    m_pDefinitionChangeEvent->Emit(&arguments);
}

bool RadioButtonGroupDefinition::SetVisible(const std::wstring& id, bool visible)
{
    auto search = std::find_if(m_radioButtonDefinitions.begin(), m_radioButtonDefinitions.end(), FindPtrBIdPredicate<OwnerPtr<RadioButtonDefinition>>(id));
    DBG_WARN_AND_RETURN_FALSE_UNLESS(search != m_radioButtonDefinitions.end(), L"m_radioButtonDefinitions内没有要找的名字",L"GDMPLab",L"2024-03-30");
    DBG_WARN_AND_RETURN_FALSE_UNLESS((*search), L"(*search)为空",L"GDMPLab",L"2024-03-30");
    if ((*search)->IsVisible() != visible)
    {
        bool res = (*search)->SetVisible(visible);
        if (res)
        {
            EmitDefineChangeEvent();
            return true;
        }
    }
    return false;
}

bool RadioButtonGroupDefinition::SetControlHandler(OwnerPtr<IRadioButtonGroupHandler> pControlHandler)
{
    DBG_WARN_AND_RETURN_FALSE_UNLESS(pControlHandler, L"pControlHandler为空",L"GDMPLab",L"2024-03-30");
    bool IsNeedEmitDefineChangeEvent = false;
    for (auto& define : m_radioButtonDefinitions)
    {
        bool visible = define->IsVisible();
        bool enabled = define->IsEnabled();
        define->SetControlHandler(pControlHandler.get());
        if (IsNeedEmitDefineChangeEvent)
            continue;

        if (visible != pControlHandler->IsVisible(define->GetId()) || enabled != pControlHandler->IsEnabled(define->GetId()))
        {
            IsNeedEmitDefineChangeEvent = true;
        }        
    }
    if (IsNeedEmitDefineChangeEvent)
    {
        EmitDefineChangeEvent();
    }
    m_handler.SetControlHandler(TransferOwnership(pControlHandler));
    return true;
}

bool RadioButtonGroupDefinition::BlockControlHandler(bool needBlock)
{
    return m_handler.BlockControlHandler(needBlock);
}

}   // end namespace gcmp
